/*
 * EP-Tracing System : a tool to trace mountain hike.           .kNNx
 * Copyright (C) 2009  Raffaello Brondi                         'KNNNk
 *                                                             ,XNNNl
 * This program is free software:                             ,XNN0'
 * you can redistribute it and/or modify                     ,XNNx.
 * it under the terms of the                                'XNNl
 * GNU General Public License                              .KNN:                     ..
 * as published by                                        .0NNo                    .ONNO.
 * the Free Software Foundation,                          xNNN.  :dkd,            .KNNNN;
 * either version 3 of the License, or                   :NNNK ;XNNNNNo          ,KNNNX:
 * (at your option) any later version.                   xNNNNcXNNNNNNk         cNNNNx.
 *                                                       ,XNNNNNNNNNNX'       .xNNNK;
 *                                                        .lKNNNNNNNK'       :KNNNo
 *                                                          ;kNNNNNKxl;'.'l0NNNk.
 * This program is distributed in                          .'  .:xXNNNNNNNNNNNO,
 * the hope that it will be useful,                       :NNXd'   ,oONNNNNNk,
 * but WITHOUT ANY WARRANTY;                              xNNNNN0c.    .;;,.
 * without even the implied warranty of                   kNNNNNNNXd'
 * MERCHANTABILITY or                                     xNNNNNNNNNNOc.
 * FITNESS FOR A PARTICULAR PURPOSE.                      xNNNNNNNNNNNNXd.
 * See the GNU General Public License                     ONNNN0.,o0NNNNNNl
 * for more details.                                      0NNNNd    kNNNNNX.
 *                                                        KNNNNl   oNNNNNXc
 *                                                       .XNNNN:  dNNNNNx.
 * You should have received a copy                       .XNNNN; lNNNNO'
 * of the GNU General Public License                     .XNNNN; 'dxo'
 * along with this program.                              .XNNNN;
 * If not, see <http://www.gnu.org/licenses/>.            0NNNN:
 *                                                        kNNNNc
 *                                                        oNNNNd
 *                                                        ;NNNN0
 * Contact: rbrondi@gmail.com                             .XNNNN;
 *                                                         kNNNNd
 *                                                         .coo:.
 */
package EP.UI;

import java.util.Timer;
import java.util.TimerTask;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/**
 *
 * @author Raffaello Brondi
 */
public class SplashScreen extends Canvas {

    private Image image;
    private Timer timer;
    private Displayable next;
    private int x;
    private int y;

    public SplashScreen(String imSource, Displayable next) {
        try {
            this.image = Image.createImage(imSource);
        } catch (java.io.IOException e) {
            e.printStackTrace();
            this.image = null;
        }
        this.next = next;
        this.timer = new Timer();

        this.x = this.getWidth()/2;
        this.y = this.getHeight()/2;
    }

    protected void keyPressed(int keyCode) {
        this.timer.cancel();
        dismiss();
    }

    protected void paint(Graphics g) {
        g.setColor(0xffffff);
        g.fillRect(0, 0, getWidth(), getHeight());

        System.out.println("paint " + image);
        g.drawImage(this.image, this.x, this.y, Graphics.HCENTER| Graphics.BOTTOM);
    }

    protected void pointerPressed(int x, int y) {
        this.timer.cancel();
        dismiss();
    }

    protected void showNotify() {
        timer.schedule(new CloseSplashScreen(), 3000);
    }

    private void dismiss() {
        EPTS.getDisplay().setCurrent(next);
    }

    // ----------------------------------------
    private class CloseSplashScreen extends TimerTask {

        public void run() {
            dismiss();
        }
    }
}
